package com.zui.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zui.admin.dao.CategoryDao;
import com.zui.admin.domain.entity.Category;
import com.zui.admin.domain.vo.*;
import com.zui.admin.service.CategoryService;
import com.zui.common.domain.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类接口
 * @author 最光阴
 * @version 2021/5/4
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    /**
     * 商品分类-添加
     *
     * @param addCategoryVO
     * @return
     */
    @Override
    public void add(AddCategoryVO addCategoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryVO, category);
        save(category);
    }

    /**
     * 分页查询商品分类
     * @param param
     * @return
     */
    public PageInfo<CategoryVO> page(PageParam<ListCategoryVO> param) {
        PageHelper.startPage(param.getPageNo(), param.getPageSize());

        List<CategoryVO> list = baseMapper.findList(param.getObj());

        return new PageInfo<>(list);
    }

    /**
     * 根据id查询商品分类详情
     *
     * @param id
     * @return
     */
    public CategoryVO get(String id) {
        Category category = baseMapper.selectById(id);
        CategoryVO result = new CategoryVO();
        BeanUtils.copyProperties(category, result);
        return result;
    }

    /**
     * 查询商品分类树
     *
     * @return
     */
    public List<CategoryVO> tree() {
        // 一、查出所有分类
        List<CategoryVO> list = baseMapper.findList(new ListCategoryVO());

        // 二、组装树结构
        List<CategoryVO> result = getTree(list);

        // 三、返回结果
        return result;
    }

    private List<CategoryVO> getTree(List<CategoryVO> list) {
        return list.stream()
                // 1. 查出一级分类
                .filter(s -> s.getLevel() == 0)
                // 2. 获取子分类
                .map(s -> {
                    s.setChilds(getChilds(s, list));
                    return s;
                })
                // 3. 给一级分类排序
                .sorted((c1, c2) -> {
                    return c1.getSort() - c2.getSort();
                })
                // 4. 组装成 List 返回
                .collect(Collectors.toList());
    }

    private List<CategoryVO> getChilds(CategoryVO category, List<CategoryVO> list) {
        List<CategoryVO> result = list.stream()
                // 1. 查找 category 的子分类
                .filter(s -> s.getPid().compareTo(category.getId()) == 0)
                // 2. 递归查找子分类
                .map(s -> {
                    s.setChilds(getChilds(s, list));
                    return s;
                })
                // 3. 给一级分类排序
                .sorted((c1, c2) -> {
                    return c1.getSort() - c2.getSort();
                })
                // 4. 组装成 List 返回
                .collect(Collectors.toList());

        return result;
    }

    /**
     * 商品分类-更新
     *
     * @param updateCategoryVO
     * @return
     */
    @Override
    public void update(UpdateCategoryVO updateCategoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryVO, category);
        updateById(category);
    }

    /**
     * 商品分类-删除
     *
     * @param deleteCategoryVO
     * @return
     */
    @Override
    public void delete(DeleteCategoryVO deleteCategoryVO) {
        // TODO ggg 判断是否有引用，有则不允许删除
        baseMapper.deleteById(deleteCategoryVO.getId());
    }
}
